#!/usr/bin/env python           # This is client.py file

import socket               # Import socket module
import select
import threading

class readMessages(threading.Thread):
    def __init__(self, server):
        threading.Thread.__init__(self)
        self.s = server
    def run(self):
        while True: 
            read_ready, write_ready, errors = select.select(self.s, self.s, empty, 5000)
            for server in read_ready:
                print server.recv(4096)
    





s = [socket.socket()]         # Create a socket object
address = raw_input("Enter ip: ")
port = int(raw_input("Enter port: "))
hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(address) # Get local machine name
print 'server hostname =', str(hostname)
empty = []
raw_input('OK? (type anything to proceed)')

s[0].connect((hostname, port))
print s[0].recv(4096)

readThread = readMessages(s)
readThread.start()

while True:
    read_ready, write_ready, errors = select.select(s, s, empty, 5000)
    for server in write_ready:
    	message = raw_input()
    	if message != '':
    	    server.send(message)

        
        
s.close()                     # Close the socket when done